package com.rui.study.commons.util;

import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

import java.io.File;
import java.util.Iterator;

public class ExcelUtil {

    public static <T> T read(String filename, SheetProcessor<T> processor) {
        try {
            File xlsFile = new File(filename);
            // 获得工作簿对象
            Workbook workbook = WorkbookFactory.create(xlsFile);
            // 获得所有工作表
            Iterator<Sheet> sheetIterator = workbook.sheetIterator();
            // 遍历工作表
            return processor.process(sheetIterator);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }


    public interface SheetProcessor<T> {

        T process(Iterator<Sheet> sheetIterator);

    }
}
